package com.qhgrain.api.menurolepermission.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

import com.paas.base.modules.utils.lang.StringUtil;
import com.qhgrain.api.menurolepermission.dao.RolePermissionMapper;
import com.qhgrain.api.menurolepermission.entity.RolePermission;
import com.qhgrain.api.menurolepermission.service.IRolePermissionService;
import com.qhgrain.gway.util.UUIDUtil;
@Service
@Transactional(rollbackFor = Exception.class)
public class RolePermissionServiceImpl implements IRolePermissionService{

	@Autowired
	private RolePermissionMapper rolePermissionMapper;
	
	@Override
	public List<RolePermission> queryByPermissionId(String permissionId) {
		
		Example e = 
				new Example(RolePermission.class);
		e.createCriteria().andEqualTo("permissionId", permissionId);
		return this.rolePermissionMapper.selectByExample(e);
	}

	@Override
	public List<RolePermission> queryByRoleId(String roleId,String appId) {
		if(StringUtil.isBlank(appId)){
			Example e = 
					new Example(RolePermission.class);
			e.createCriteria().andEqualTo("roleId", roleId);
			return this.rolePermissionMapper.selectByExample(e);
		}else{
			return this.rolePermissionMapper.queryByRoleId(roleId, appId);
		}
		
	}

	@Transactional
	@Override
	public void addAndDelete(String roleId, List<String> addPermissionIds,
			List<String> deletePermissionIds) {
		
		if(deletePermissionIds != null && deletePermissionIds.size() > 0){
			this.rolePermissionMapper.deleteBatch(roleId, deletePermissionIds);
		}
		if(addPermissionIds != null && addPermissionIds.size() > 0){
			List<RolePermission> list = new ArrayList<RolePermission>();
			for(String pid: addPermissionIds){
				list.add(new RolePermission(UUIDUtil.getUUID(),roleId,pid));
			}
			this.rolePermissionMapper.insertBatch(list);
		}
	}

}
